//
//  ViewController.m
//  网络获取图片
//
//  Created by student on 16/3/22.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic,strong) NSArray *arr;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.arr = @[@"http://www.apple.com.cn/mac-pro/images/static/processor.jpg",@"http://www.apple.com.cn/mac-pro/images/static/memory.jpg",@"http://www.apple.com.cn/mac-pro/images/static/graphics.jpg",@"http://www.apple.com.cn/mac-pro/images/static/storage.jpg",@"http://www.apple.com.cn/mac-pro/images/static/thermal.jpg",@"http://www.apple.com.cn/mac-pro/images/static/fan.jpg",@"http://www.apple.com.cn/mac-pro/images/static/expansion.jpg",@"http://www.apple.com.cn/mac-pro/images/static/wifi.jpg",@"http://www.apple.com.cn/mac-pro/images/static/design.jpg",@"http://www.apple.com.cn/mac-pro/images/static/comingsoon.jpg"];
    
    // 练习要求:
    // 使用gcd及performSelectorInBackground方法实现多线程下载图片并显示到界面上,不能卡主界面,并且gcd采用不同的方式(串行、并发)都实现一下
    
//    // 网址字符串
//    NSString *urlStr = @"http://www.apple.com.cn/mac-pro/images/static/processor.jpg";
//    // 网址对象
//    NSURL *url = [NSURL URLWithString:urlStr];
//    // 获取数据
//    NSData *data = [NSData dataWithContentsOfURL:url];
//    // 图像对象
//    UIImage *image = [UIImage imageWithData:data];
//    // 图像视图
//    UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
//    imgView.frame = CGRectMake(100, 100, 100, 100);
//    // 添加到视图
//    [self.view addSubview:imgView];
    
    
}
- (IBAction)btnPressed:(id)sender
{
    //开始时间
    NSDate *d1 = [NSDate date];
    
    [self.activityView startAnimating];
    //GCD
    dispatch_queue_t queue = dispatch_queue_create(NULL, NULL);
    dispatch_async(queue, ^{
        int x = 50;
        int y = 100;
        for (int i=0; i<10; i++)
        {
            //网址对象
            NSURL *url = [NSURL URLWithString:self.arr[i]];
            //获取数据
            NSData *data = [NSData dataWithContentsOfURL:url];
            //图像对象
            UIImage *image = [UIImage imageWithData:data];
            //图像视图
            UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
            imgView.frame = CGRectMake(x, y, 100, 100);
            if(x<200)
            {
                x+=100;
            }
            else
            {
                x = 50;
                y+=100;
            }
            [self.view addSubview:imgView];
            // 停止转动
            [self.activityView stopAnimating];
            //运行完时间
            NSDate *d2 = [NSDate date];
            //计算总共运行了多少时间
            NSTimeInterval interval = [d2 timeIntervalSinceDate:d1];
            NSLog(@"运行了%.3f秒",interval);
        }
    });
}

- (IBAction)btn2Pressed:(id)sender
{
    //开始时间
    NSDate *d1 = [NSDate date];
    
    [self.activityView startAnimating];
    //GCD
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_async(queue, ^{
        int x = 50;
        int y = 100;
        for (int i=0; i<10; i++)
        {
            //网址对象
            NSURL *url = [NSURL URLWithString:self.arr[i]];
            //获取数据
            NSData *data = [NSData dataWithContentsOfURL:url];
            //图像对象
            UIImage *image = [UIImage imageWithData:data];
            //图像视图
            UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
            imgView.frame = CGRectMake(x, y, 100, 100);
            if(x<200)
            {
                x+=100;
            }
            else
            {
                x = 50;
                y+=100;
            }
            [self.view addSubview:imgView];
            // 停止转动
            [self.activityView stopAnimating];
            //运行完时间
            NSDate *d2 = [NSDate date];
            //计算总共运行了多少时间
            NSTimeInterval interval = [d2 timeIntervalSinceDate:d1];
            NSLog(@"运行了%.3f秒",interval);
        }
    });
}

- (IBAction)btn3Pressed:(id)sender
{
    // 把某个任务放到后台线程执行
    [self.activityView startAnimating];
    [self performSelectorInBackground:@selector(addImageView) withObject:nil];
}

- (void)addImageView
{
    int x = 50;
    int y = 100;
    for (int i=0; i<10; i++)
    {
        //网址对象
        NSURL *url = [NSURL URLWithString:self.arr[i]];
        //获取数据
        NSData *data = [NSData dataWithContentsOfURL:url];
        //图像对象
        UIImage *image = [UIImage imageWithData:data];
        //图像视图
        UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
        imgView.frame = CGRectMake(x, y, 100, 100);
        if(x<200)
        {
            x+=100;
        }
        else
        {
            x = 50;
            y+=100;
        }
        [self.view addSubview:imgView];
    }
    [self performSelectorOnMainThread:@selector(showResult) withObject:nil waitUntilDone:NO];
}

- (void)showResult
{
    [self.activityView stopAnimating];
}
@end
